<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--
        <blockquote class="layui-elem-quote">
            Layui的树形表格treeTable，支持异步加载(懒加载)、复选框联动、折叠状态记忆。<br>
            <a href="https://gitee.com/whvse/treetable-lay" target="_blank" class="layui-btn layui-btn-danger">treetable-lay</a>
        </blockquote>
        -->
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
        
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="add"> 添加 </a>
</script>

<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '/?s=Portal.Page.Menu',
            page: false,
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.ret == 200 ? 0 : res.ret, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.menus //解析数据列表
                };
            },
            cols: [[
                {type: 'numbers'},
                {field: 'authorityName', minWidth: 60, title: '菜单标题'},
                {field: 'authority', title: '菜单ID'},
                {field: 'menuUrl', title: '菜单url'},
                {field: 'orderNumber', width: 80, align: 'center', title: '排序'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.parentId == 0) {
                            return '<span class="layui-badge layui-bg-blue">顶级菜单</span>';
                        } else {
                            return '<span class="layui-badge-rim">左侧菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', minWidth: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.msg('TODO 删除' + data.id);
                layer.confirm("是否确认要删除此菜单？\n\n" + data.authorityName, function (index) {

                    $.ajax({
                        url: '/?s=Portal.Page.DeleteMenu',
                        type:'post',
                        data: {id: obj.data.id},
                        success:function(data){
                            if (data.ret != 200) {
                                layer.msg(data.msg);
                                return;
                            }

                            layer.msg('删除成功', function () {
                            });
                            obj.del();
                            layer.close(index);
                        },
                        complete: function () {
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                sessionStorage.setItem('edit_id', data.id); // HACK
                
                var index = layer.open({
                    title: '修改菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './phalapi-menu/edit.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (layEvent === 'add') {
                sessionStorage.setItem('add_parent_id', obj.data.id); // HACK
                
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './phalapi-menu/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>
